<?php
if (!defined('ipos')) die("Please contact Y!m: ichphien_pro or Email: ichphien_pro@yahoo.com.");

// update tag search google + yahoo
function autotag($phim_id) {
	global $db;
	if($_SERVER['HTTP_REFERER']) {
		$tagurl		=	$_SERVER['HTTP_REFERER'];
		$url_code	=	explode("/",$tagurl);
		$domainl	=	$url_code[2];
		// kiếm tra domain
		if(in_array($domainl, array('www.google.com.vn',
										'www.google.com',
										'google.com',
										'google.com.vn',
										'go.google.com',
										'images.google.com',
										'video.google.com',
										'news.google.com',
										'blogsearch.google.com',
										'maps.google.com',
										'local.google.com'))) {
				$keyworld	=	explode("q=",$tagurl);
				$keyworld	=	explode("&",$keyworld[1]);
				$keyworld	=	htmlchars(stripslashes(urldecode($keyworld[0])));
				// tiến hành install tag mới
				$tagup		=	$db->database("phimtag","film_info","phimid = '$phim_id'");
				$checktag	=	explode($keyworld.',',$tagup[0][0]);
				if(count($checktag) > 1) {
					$tagupload	=	$keyworld.','.$tagup[0][0];
					$db->dbupdate("film_info","phimtag='$tagupload'","phimid = '$phim_id'");
				}
		}
	}
	// end	
}
// thể loại
function get_category($list,$num=0) {
	global $db;
	$list	=	substr($list, 1);
	$list	=	substr($list,0,-1);
	$cat	=	$db->database("danhmucten","category","danhmucid IN (".$list.")");
	for($i=0;$i<count($cat);$i++) {
		$name_cat		=	$cat[$i][0];
		$url_category	=	url_link($name_cat,0,'the-loai');
		$html  .=	"<a href=\"$url_category\" title=\"Phim $name_cat\">$name_cat</a>, ";
	}
	$html 	= substr($html,0,-2);
	return	$html;
}

function nav_category($list) {
	global $db;
	$list	=	substr($list, 1);
	$list	=	substr($list,0,-1);
	$cat	=	$db->database("danhmucten","category","danhmucid IN (".$list.")");
	for($i=0;$i<count($cat);$i++) {
		$name_cat		=	$cat[$i][0];
		$url_category	=	url_link($name_cat,0,'the-loai');
		$html  .=	"<span typeof=\"v:Breadcrumb\">
							<a property=\"v:title\" rel=\"v:url\" href=\"$url_category\" title=\"Phim $name_cat\">$name_cat</a>
					</span> » ";
	}
	return	$html;
}

// quốc gia
function get_country($ipid) {
	global $db;
	$country	=	$db->database("quocgiaten","country","quocgiaid = '$ipid'");
	return $country[0][0];
}
// install tag
function install_tags($code) {
	$code 	= str_replace(",,,,",",",$code);
	$code 	= str_replace(",,,",",",$code);
	$code 	= str_replace(",,",",",$code);
	$tags 	= get_ascii($code);
	return $tags;
}
// tags html
function get_tags($name,$list,$z=0) {
	if($list) {
		 $list = str_replace(",  ",",",$list);
		 $list = str_replace(", ",",",$list);
		 $s = explode(',',$list);
		 if($z == 0)	$cbxz	= ", ";	
		 foreach($s as $x=>$val) {
			$tname		=	replace($s[$x]);
			$url_tags	=	"$name/".str_replace("-","+",$tname).".html";
			$html_cat  .=	"<a href=\"".$url_tags."\" title=\"".$s[$x]."\">".$s[$x]."</a>$cbxz";
		 }
		  if($z == 0) 
		  	$html_cat 		= substr($html_cat,0,-2);
		return $html_cat;
	}else {
		return false;
	}
}
// tên nhóm
function groupName($num) {
	if($num == 0)		$name	=	"<span>Thành viên thường</span>";
	elseif($num == 1)	$name	=	"<span style=\"color:blue\">Thành viên VIP</span>";
	elseif($num == 2)	$name	=	"<span style=\"color:green\">Quản lý</span>";
	elseif($num == 3)	$name	=	"<span style=\"color:red\">Administrator</span>";
	return $name;	
}
// giới tính
function sexName($num) {
	if($num == 0)		$name	=	"Nam";
	elseif($num == 1)	$name	=	"Nữ";
	return $name;	
}

// server
function server_ep($phimid,$phimname,$tentienganh,$server) {
	global $db;
	
	$tap	 	=	$db->database("epid,epname,epurl","ep","phimid = '$phimid' ORDER BY epid ASC");
$episode_type = $tap[0][2];
$total_server	.=	"<div class=\"eplist\" id=\"_listep\"><p class=\"listep\" id=\"sv_$server\">";
for($x=0;$x<count($tap);$x++) {
$epid		=	$tap[$x][0];
$epname		=	$tap[$x][1];
$playLink	=	url_link($phimname."-".$tentienganh,$epid,"xem-phim");
$episode_type = type_video($tap[$x][2]);
$sv[$episode_type] .= "<a id=\"ep_$epid\" href=\"$playLink\" onclick=\"setupplayer($epid,$server);return false;\" title=\"Xem phim $phimname tập $epname\">$epname</a>";
}
if($sv[1]) $total_server .= "<span class='svname'>V.I.P:</span><span class='svep'>".$sv[1]."</span><br />";
if($sv[2]) $total_server .= "<span class='svname'>Flash:</span><span class='svep'>".$sv[2]."</span><br />";
if($sv[3]) $total_server .= "<span class='svname'>Zing:</span><span class='svep'>".$sv[3]."</span><br />";
if($sv[4]) $total_server .= "<span class='svname'>YouTube:</span><span class='svep'>".$sv[4]."</span><br />";
if($sv[5]) $total_server .= "<span class='svname'>Picasa:</span><span class='svep'>".$sv[5]."</span><br />";
if($sv[6]) $total_server .= "<span class='svname'>Movshare:</span><span class='svep'>".$sv[6]."</span><br />";
if($sv[7]) $total_server .= "<span class='svname'>Tam Tay:</span><span class='svep'>".$sv[7]."</span><br />";
if($sv[8]) $total_server .= "<span class='svname'>4Share:</span><span class='svep'>".$sv[8]."</span><br />";
if($sv[9]) $total_server .= "<span class='svname'>Unknow:</span><span class='svep'>".$sv[9]."</span><br />";
if($sv[10]) $total_server .= "<span class='svname'>2Share:</span><span class='svep'>".$sv[10]."</span><br />";
if($sv[11]) $total_server .= "<span class='svname'>Clip.Vn:</span><span class='svep'>".$sv[11]."</span><br />";
if($sv[12]) $total_server .= "<span class='svname'>Ban Be:</span><span class='svep'>".$sv[12]."</span><br />";
if($sv[13]) $total_server .= "<span class='svname'>Veoh:</span><span class='svep'>".$sv[13]."</span><br />";
if($sv[14]) $total_server .= "<span class='svname'>MegaFun:</span><span class='svep'>".$sv[14]."</span><br />";
if($sv[15]) $total_server .= "<span class='svname'>NhacCuaTui:</span><span class='svep'>".$sv[15]."</span><br />";
if($sv[16]) $total_server .= "<span class='svname'>Dailymotion:</span><span class='svep'>".$sv[16]."</span><br />";
if($sv[17]) $total_server .= "<span class='svname'>Zippy Share:</span><span class='svep'>".$sv[17]."</span><br />";
if($sv[18]) $total_server .= "<span class='svname'>YouTube:</span><span class='svep'>".$sv[18]."</span><br />";
if($sv[19]) $total_server .= "<span class='svname'>Cyworld:</span><span class='svep'>".$sv[19]."</span><br />";
if($sv[20]) $total_server .= "<span class='svname'>Gdata:</span><span class='svep'>".$sv[20]."</span><br />";
$total_server	.=	"</span></p></div>";
	return $total_server;
}

// bình luận list
function comment($phimid,$page) {
	global $db;
	$num		= 6;
	$num 		= intval($num);
	$page 		= intval($page);
	if (!$page) $page = 1;
	$limit 		= ($page-1)*$num;
	if($limit<0) $limit=0;

	$arr	=	$db->database("userid,camnhantime,camnhantxt","comment","phimid = '$phimid' order by camnhanid desc limit $limit,$num");
	$total	=	count($db->database("camnhanid","comment","phimid = '$phimid' order by camnhanid desc"));
	$pagehtml	=	ipos_pages($total,$num,$page,$phimid,"comment");
	if($arr) {
		for($i=0;$i<count($arr);$i++) {
			$user	=	$db->database("username,avatar","user","userid = '".$arr[$i][0]."'");
			$usern	 =	$user[0][0];
			$avt	 =	avatar($user[0][1],50);
			$urln	 =	url_link($usern,$arr[$i][0],"user");
			$html	.=	'<div class="cm_list">
                         	<div class="avatar"><img src="'.$avt.'"></div>
                            <div class="contents">
                            	<p class="username"><a href="'.$urln.'">'.$usern.'</a><span>Vào lúc '.GetTIMEDATE($arr[$i][1]).'</span></p>
                                <p class="cmtxt">'.txtbr($arr[$i][2]).'</p>
                            </div>
                            <div class="clr"></div>
                         </div>';
		}
		$html	.=	'<div style="height:40px;"></div><ul class="onervn_pages">'.$pagehtml.'</ul>';
	}
	return	$html;
}
function comment_phim($phimid, $page) {
	global $db;
	$num		= 10;
	$num 		= intval($num);
	$page 		= intval($page);
	if (!$page) $page = 1;
	$limit 		= ($page-1)*$num;
	if($limit<0) $limit=0;
	$comment	=	$db->database("userid,camnhantime,camnhantxt","comment","phimid = '$phimid' order by camnhanid desc limit $limit,$num");
	$total	=	count($db->database("camnhanid","comment","phimid = '$phimid' order by camnhanid desc"));
	$pagehtml	=	ipos_pages($total,$num,$page,$phimid,"comment");
	$user	=	$db->database("avatar","user","userid = '".$_SESSION["ipos_userid"]."'");
	$avatar			=	$user[0][0];
	$html .= '<div class="comment">
	<div class="fcomment">
		<div class="avatar"><img src="'.avatar($avatar).'" /></div>
		<form name="commentz" id="_frmLogin" onsubmit="return false;">
		<textarea name="comment" rows="4" placeholder="Hãy chia sẻ cảm nghĩ của bạn về phim này."></textarea>
		<input type="hidden" name="phimid" value="'.$phimid.'">
		<p>Bình luận phải dài hơn 30 ký tự.</p>
		<a href onclick="return commentsubmit();" class="button-comment">Bình luận</a>
	</form>
	</div>';
	$html .= '<div class="lcomment">
		<ul>';
	if (!$comment) { $html .= 'Chưa có bình luận nào, hãy bình luận góp phần phát triển website';
	} else {
	for($i=0;$i<count($comment);$i++) {
		$user_comment	=	$db->database("userid, username, avatar, usergroup","user","userid = '".$comment[$i][0]."'");
		$_name		=	$user_comment[0][1];
		$_avatar		=	$user_comment[0][2];
		$_url	=	url_link($_name,$user_comment[0][0],'thanh-vien');
		if ($user_comment[0][3] == 1) { $rank = '<span class="rank_member vip"></span>';
		} else { $rank = '<span class="rank_member member"></span>'; }
			$html .= '<li>
			<div class="avatar">'.$rank.'<img src="'.avatar($_avatar).'"/></div>
				<div class="content">
				<p class="name"><strong><a href="'.$_url.'">'.$_name.'</a></strong><span>'.(date("G:i:s d/m/Y",$comment[$i][1])).'</span></p>
				<p class="comment">'.nl2br($comment[$i][2]).'</p>
				</div>
			</li>';
	}
	}
	$html .= '</ul></div><div class="paging">'.$pagehtml.'</div></div>';
	return $html;
}
?>